package cn.edu.neusoft.liushuai702.weatherdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import cn.edu.neusoft.liushuai702.weatherdemo.bean.WeatherInfo;
import cn.edu.neusoft.liushuai702.weatherdemo.iface.WeatherListener;
import cn.edu.neusoft.liushuai702.weatherdemo.model.Weather51Model;
import cn.edu.neusoft.liushuai702.weatherdemo.model.WeatherModel;

public class MainActivity extends AppCompatActivity implements View.OnClickListener,WeatherListener{
    TextView textView;
    Button button_weather,button_51;
    EditText editText;
    TextView tv_city;
    TextView tv_cityid;
    TextView tv_WD;
    TextView tv_SD;

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText = (EditText)findViewById(R.id.editText);
        button_weather = (Button)findViewById(R.id.button);
        button_51 = (Button)findViewById(R.id.button2);
        textView = (TextView)findViewById(R.id.textView3);
        button_weather.setOnClickListener(this);
        button_51.setOnClickListener(this);
        tv_city = (TextView)findViewById(R.id.textView5);
        tv_cityid =(TextView)findViewById(R.id.textView7);
        tv_WD = (TextView)findViewById(R.id.textView9);
        tv_SD = (TextView)findViewById(R.id.textView11);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button:
                WeatherModel weatherModel = new WeatherModel();
                weatherModel.getWeather(editText.getText().toString(),this);
                break;
            case R.id.button2:
                Weather51Model weather51Model = new Weather51Model();
                weather51Model.getWeather(editText.getText().toString(),this);
                break;
            default:
                break;
        }
    }

    @Override
    public void onResponse(WeatherInfo weatherInfo) {
        textView.setText(weatherInfo.getTemp());
        tv_city.setText(weatherInfo.getCity());
        tv_cityid.setText(weatherInfo.getCityid());
        tv_WD.setText(weatherInfo.getWD());
        tv_SD.setText(weatherInfo.getSD());
    }

    @Override
    public void onFail(String msg) {
        textView.setText("error");
    }
}
